package com.example.demo.web;

import com.alibaba.fastjson.JSON;
import com.example.demo.mapper.firstsqlserver.GroupMapper;
import com.example.demo.pojo.Group;
import com.example.demo.pojo.GroupInfo;
import com.example.demo.pojo.GroupMember;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
public class GroupController {

    @Autowired
    private GroupMapper groupMapper;

    @RequestMapping("/listGroup")
    public String listGroup(Model model) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String date = sdf.format(new Date());
        List<Group> all = groupMapper.getGroupInfo(date);
        /*PageHelper.startPage(1, 10);
        List<Group> groups = groupMapper.getGroupInfo(date);*/
        for (int i = 0; i < all.size(); i++) {
            Group group = all.get(i);
            List<GroupMember> members = groupMapper.getTotalPers(group.getgId(), date);
            System.out.println("gId=" + group.getgId() + ",size=" + members.size());
            group.setgPers(members.size());
            double average = (double)group.getSuccess() / members.size();
            DecimalFormat df = new DecimalFormat("0.00");
            group.setAverage(Double.parseDouble(df.format(average)));
        }
        /*PageInfo<Group> pages = new PageInfo<>(groups);
        for (int i = 0; i < pages.getList().size(); i++) {
            Group group = pages.getList().get(i);
            group.setTotal((int)pages.getTotal());
        }*/
//        String data = JSON.toJSONString(pages.getList());
        String alls = JSON.toJSONString(all);
//        model.addAttribute("groups", data);
        model.addAttribute("today", date);
        model.addAttribute("all", alls);
        return "listGroup";
    }

    @ResponseBody
    @RequestMapping("/listTodayGroupData")
    public List<Group> listGroupData(@RequestParam(value = "timer")String timer) {
       /* SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String date = sdf.format(new Date());
        PageHelper.startPage(start, size);*/
        List<Group> groups = groupMapper.getGroupInfo(timer);
        for (int i = 0; i < groups.size(); i++) {
            Group group = groups.get(i);
            List<GroupMember> members = groupMapper.getTotalPers(group.getgId(), timer);
            System.out.println("gId=" + group.getgId() + ",size=" + members.size());
            group.setgPers(members.size());
            double average = (double)group.getSuccess() / members.size();
            DecimalFormat df = new DecimalFormat("0.00");
            group.setAverage(Double.parseDouble(df.format(average)));
        }
//        PageInfo<Group> pages = new PageInfo<>(groups);
        return groups;
    }
}
